﻿using EasyNet.Componets.Core.Inject;
using EasyNet.Componets.Core.Mapper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Utils
{
    
    public static class MapperUtil
    {
        private static IMapper _instance;

        private static IMapper Instance
        {
            get
            {
                if (_instance != null)
                {
                    return _instance;
                }
                try
                {
                    _instance = IoC.Resolve<IMapper>();
                }
                catch
                {
                    _instance = new EasyNetMapper();
                }

                return _instance;
            }
        }
        
        /// <summary>
        /// 设置实例
        /// </summary>
        /// <param name="mapper"></param>
        public static void SetInstance(IMapper mapper)
        {
            _instance = mapper;
        }

        public static TTarget Map<TSource, TTarget>(TSource source)
            where TTarget : class, new()
            where TSource : class
        {
            return Instance.Map<TSource, TTarget>(source);
        }

        public static TTarget Map<TSource, TTarget>(TSource source, TTarget target)
         where TTarget : class, new()
         where TSource : class
        {
            return Instance.Map<TSource, TTarget>(source, target);
        }
    }
}
